<?php
class BrandsController extends AppController {
	var $name = 'Brands';
	var $uses = array('Brand');
	var $layout = 'admin';
	var $helpers = array('Html', 'Javascript', 'Form', 'Time');
	var $components = array('General','Upload');
	
	function index() {
		$condition = array();
		//Set id for case to edit
		$brand_id = null;
		
		//Get variable from url params
		if (!empty($this->passedArgs))
		{
			if (isset($this->passedArgs['Search.name']))
			{
				$condition[]["Brand.name LIKE"] = "%{$this->passedArgs['Search.name']}%";
				$this->data['Search']['name'] = $this->passedArgs['Search.name'];
			}			
		}
		//Save or edit data
		if (!empty($this->data) && isset($this->data['Brand']))
		{
			$this->Brand->id = $brand_id;
			if ($this->Brand->save($this->data))
			{
				$this->Session->setFlash('Brand đã được sửa');
				
				//Unset data brand
				unset($this->data['Brand']);
				unset($this->passedArgs[0]);
				
				//Set agains url
				$url = $this->passedArgs;
				$url['action'] = 'index';
				$this->redirect($url, null, true);
			} else {
				$this->Session->setFlash('Lưu thông tin thất bại');
			}
		}
		if (!isset($this->data['Brand']))
		{
			$this->data = $this->Brand->findById($brand_id);
		}
		
		//Get data for view index
		$this->Brand->recursive = 1;
		$this->paginate = array('limit' => 15, 'page' => 1, 'order' => array('Brand.created' => 'desc'));
		$brands = $this->paginate('Brand', $condition);
		$this->set('brands', $brands);
	}
	function add()
	{
		if (!empty($this->data))
		{
			$this->data['Brand']['created'] = date('d/m/y');
			//debug($this->data['Brand']['logo']['name']);
			//die();
			if ($this->data['Brand']['logo']['name'] != '')
			{
				//Upload anh, tao thumbnail va luu du lieu vao csdl
				$this->Upload->upload($this->data['Brand']['logo'], 'img/');
				$this->data['Brand']['logo'] = $this->data['Brand']['logo']['name'];
				$images[] =  $this->data['Brand']['logo']['name'];
			}   
			//Khong chon anh cho san pham
			//luu du lieu vao csdl binh thuong
			else
			{
				$this->data['Brand']['logo'] = 'noimage';
			}
			if($this->Brand->save($this->data))
			{
				$this->Session->setFlash (__ ('Đã thêm được', true));
			}else
			{
				$this->Session->setFlash (__ ('Không thêm được', true));
			}
		}
	}
	function edit($id = null)
	{
		if(!$id && empty($this->data))
		{
			//error
		}
		
		else if(!empty($this->data))
		{
			$this->data['Brand']['created'] = date('d/m/y');
			
			if ($this->data['Brand']['logos']['name'])
			{
				if (!$this->Upload->upload($this->data['Brand']['logos'], WWW_ROOT . 'img' .DS))
				{
				$this->Session->setFlash('Kh™ng Upload được ảnh');
				$this->redirect($this->referer());
				}
				@unlink (WWW_ROOT . 'img'. DS . $adv['Brand']['logo']);
				$this->data['Brand']['logo'] = $this->data['Brand']['logos']['name'];
			} 
			//Khong chon anh cho san pham
			//luu du lieu vao csdl binh thuong
			
			if($this->Brand->save($this->data))
			{
				$this->Session->setFlash (__ ('Đã thêm được', true));
			}else
			{
				$this->Session->setFlash (__ ('Không thêm được', true));
			}
		}
		if(empty ($this->data))
		{
			$this->data = $this->Brand->findById($id);
		}
	}
	function delete($id = null)
	{
		if (!$id)
		{
			$this->Session->setFlash('Không đúng ID');
			$this->redirect ('index');
		}
		if ($this->Brand->del($id))
		{
			$this->Session->setFlash('Đã xóa xong');
		}
		$this->redirect('index');
	}
}
?>